/**
 * Created by zzy on 2017/1/13.
 */
var $ = require('jquery');
require('layout/util');
var $alert = require('libs/alert');
var $form = require('js-path/formValide.js');
var $form2 = require('js-path/formValideNotSub.js');
var col = 5;
var mms = 3;
$(function(){
    pageInit();
    mainFormValidInit();
});
function pageInit() {
    var id = $.getUrlParam('id');
    loadAndInit(id);
    $("#main_form input[name=systemId]").on("change",function () {
        var systemId = $(this).val();
        authoritySimpleQueryAjax(systemId);
    })
}
function authoritySimpleQueryAjax(systemId) {
    $.ajax({
        url:"api/v1/p/mms/authAuthority/queryAuthority",
        type:"post",
        data:{
            systemId:systemId
        },
        dataType:"json",
        success:function (result) {
            if(result.code==200){
                authorityInit(result.data);
            }else{
                $alert(result.message);
            }
        }
    })
}
function loadAndInit(id) {
    $.ajax({
        url:'api/v1/p/mms/authRole/load',
        type:'post',
        data:{
            id:id
        },
        dataType:"json",
        success:function (result) {
            if(result.code==200){
                var data = result.data;
                dataInit(data);
            }else{
                $alert(result.message);
            }
        }
    })
}
function dataInit(data) {
    $("#id").val(data.id);
    $("#role_name").val(data.roleName);
    $("#role_desc").val(data.roleDesc);
    $("#issys").val(data.issys);
    $.each($("#main_form input[name=systemId]"),function (k,o) {
        if($(o).val()==data.systemId){
            $("#main_form input[name=systemId]").eq(k).attr("checked","checked");
            return;
        }
    });
    $.ajax({
        url:"api/v1/p/mms/authAuthority/queryAuthority",
        type:"post",
        data:{
            systemId:data.systemId
        },
        dataType:"json",
        success:function (result) {
            if(result.code==200){
                authorityInit(result.data,data.roleAuthorityDos);
            }else{
                $alert(result.message);
            }
        }
    })
}
function authorityInit(data,defaultCheckedValue) {
    var authorityIds = new Array();
    if(defaultCheckedValue!=undefined){
        $.each(defaultCheckedValue,function (k,o) {
            authorityIds.push(o.authorityId);
        })
    }
    $("#div_authority").html("");
    var len = data.length;
    if(len!=0){
        var divArray = new Array(Math.floor((len-1)/col)+1);
        $.each(divArray,function (k,o) {
            divArray[k] = $("<div style='padding-left: 15px'></div>");
        });
        $.each(data,function (k,o) {
            if($.inArray(data[k].id, authorityIds)==-1){
                divArray[Math.floor(k/col)].append("<input type='checkbox' name='authorityId' value='"+data[k].id+"'><span style='margin-right: 10px'>"+data[k].name+"</span>");
            }else{
                divArray[Math.floor(k/col)].append("<input type='checkbox' name='authorityId' value='"+data[k].id+"' checked><span style='margin-right: 10px'>"+data[k].name+"</span>");
            }
        });
        $.each(divArray,function (k,o) {
            $("#div_authority").append(divArray[k]);
        });
    }
}
function customValid() {
    var authority = true;
    var len = $("#main_form input[type=checkbox]:checked").length;
    if(len==0){
        $("#div_authority").parent().parent().addClass("has-error");
        $("#div_authority").parent().next("div").html('<p id="authority-error" class="error">权限不能为空</p>');
        authority = false;
    }
    return authority;
}
function mainFormValidInit(){
    $form({selector:'#main_form'},{
        roleName:{required:true},issys:{required:true},
        systemId:{required:true}
    },{roleName:{required:'角色名称不能为空'},issys:{required:'是否系统级别不能为空'},
        systemId:{required:'所属系统不能为空'}
    },function () {
        if(!customValid()){
            return false;
        }
        var json = $('#main_form').serializeObject();
        var authIds = json.authorityId;
        var roleAuthorityDos = new Array();
        if(typeof authIds == "object"){
            $.each(authIds,function (k,o) {
                roleAuthorityDos.push({"id":o});
            });
        }else{
            roleAuthorityDos.push({"id":authIds});
        }
        json.roleAuthorityDos = roleAuthorityDos;
        delete json.authorityId;
        json.systemId = $("#main_form input[type=radio]:checked").val();
        return json;
    },function (result) {
        if(result.code == 200){
            window.location.href="role-manage";
        }else{
            $alert(result.message);
        }
    })
}